package com.xr.miniprogram.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xr.miniprogram.domain.MiniProgramService;
import com.xr.miniprogram.domain.vo.MiniProgramServiceVO;
import com.xr.miniprogram.mapper.MiniProgramServiceMapper;
import com.xr.miniprogram.mapper.MiniProgramStandardsMapper;
import com.xr.miniprogram.service.IMiniProgramServiceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class IMiniProgramServiceServiceImpl extends ServiceImpl<MiniProgramServiceMapper, MiniProgramService> implements IMiniProgramServiceService {
    @Resource
    MiniProgramServiceMapper serviceMapper;
    @Resource
    MiniProgramStandardsMapper standardsMapper;
    @Override
    public MiniProgramService getServiceByMenuId(Long menuId) {
        return this.getOne(new QueryWrapper<MiniProgramService>().lambda().eq(MiniProgramService::getMenuId, menuId));
    }

    @Override
    public MiniProgramService getServiceByServiceId(Long serviceId) {
        return this.getById(serviceId);
    }

    @Override
    public boolean addService(MiniProgramService miniProgramService) {
        return this.save(miniProgramService);
    }

    @Override
    public boolean updateService(MiniProgramService miniProgramService) {
        return this.updateById(miniProgramService);
    }

    @Override
    public List<MiniProgramServiceVO> getPopular() {
        List<MiniProgramServiceVO> miniProgramServiceVOList = new ArrayList<>();

        for (MiniProgramService miniProgramService : this.list()) {
            MiniProgramServiceVO miniProgramServiceVO = MiniProgramServiceVO.toMiniProgramServiceVO(miniProgramService);
            miniProgramServiceVO.setServiceSum(serviceMapper.countServiceOrder(miniProgramService.getServiceId()));
            miniProgramServiceVOList.add(miniProgramServiceVO);
        }

        return miniProgramServiceVOList.stream()
                .sorted(Comparator.comparing(MiniProgramServiceVO::getServiceSum).reversed())
                .limit(4)
                .collect(Collectors.toList());
    }

    @Override
    public List<MiniProgramServiceVO> getRecommendation() {
        ArrayList<MiniProgramServiceVO> miniProgramServiceVOs = new ArrayList<>();
        //查询所有有折扣的 规格id
        ArrayList<Long> list = standardsMapper.getServiceIdsIsDiscount();
        HashSet<Long> serviceIds = new HashSet<>(list);
        for (Long id : serviceIds) {
            MiniProgramServiceVO miniProgramServiceVO = MiniProgramServiceVO.toMiniProgramServiceVO(serviceMapper.selectById(id));
            miniProgramServiceVO.setServiceSum(serviceMapper.countServiceOrder(id));
            miniProgramServiceVOs.add(miniProgramServiceVO);
        }

        return miniProgramServiceVOs;
    }

    @Override
    public Long serviceOrderNumById(Long serviceId) {
        return serviceMapper.countServiceOrder(serviceId);
    }

}
